package fr.pud.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class Tools4Files {
    public static double[][] readDistanceMatrix(String fileName) {
        double[][] toReturn = null;
        try {
            // Variable definition
            int lineCounter = 0;
            String line;
            Scanner fileReader = new Scanner(new FileReader(fileName));
            while (fileReader.hasNextLine()) {
                line = fileReader.nextLine();
                String[] stringArray = line.split(";");
                if (toReturn == null) {
                    toReturn = new double[stringArray.length][stringArray.length];
                }
                for (int i = 0; i < stringArray.length; i++) {
                    toReturn[lineCounter][i] = Double
                            .parseDouble(stringArray[i]);
                }
                lineCounter++;
            }
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return toReturn;
    }

    public static void writeNodes(String filename, ArrayList<Long> list) {
        FileWriter f;
        try {
            f = new FileWriter(filename);
            for (int i = 0; i < list.size(); i++) {
                f.write(list.get(i) + "\n");
            }
            f.flush();
            f.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void writeNodes(String filename, long[] list) {
        FileWriter f;
        try {
            f = new FileWriter(filename);
            for (int i = 0; i < list.length; i++) {
                f.write(list[i] + "\n");
            }
            f.flush();
            f.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public double[][] readDist(FileReader f, int nbLin, int nbCol) {
        double[][] tab = new double[nbLin][nbCol];
        String separator = " ";
        try {
            BufferedReader b = new BufferedReader(f);
            String line = "";
            line = b.readLine();
            if (line == null) {
                System.out.println("File is empty.");
            }
            else {
                int i = 0;
                while (line != null) {
                    String[] valuesLine = line.split(separator);
                    for (int j = 0; j < tab.length; j++) {
                        tab[i][j] = Double.parseDouble(valuesLine[j]);
                    }
                    i = i + 1;
                    line = b.readLine();
                }
            }
        }
        catch (Exception e) {
            System.out.println(e.toString());
        }
        return tab;
    }

    public File writeNodes(ArrayList<Long> tab) {
        File fic = new File("nodes.txt");
        try {
            FileWriter f = new FileWriter(fic);
            BufferedWriter b = new BufferedWriter(f);
            PrintWriter p = new PrintWriter(b);
            for (int i = 0; i < tab.size(); i++) {
                p.println(tab.get(i));
            }
            p.close();
            System.out.println("Dates are safeguarded correctly.");
        }
        catch (Exception e) {
            System.out.println(e.toString());
        }
        return fic;
    }
}
